package com.fcg.redis.topone.controller;

import com.fcg.redis.rediscommon.bean.ResponseBean;
import com.fcg.redis.topone.bean.MsgDto;
import com.fcg.redis.topone.bean.MsgType;
import com.fcg.redis.topone.service.ReadService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author glxx
 */
@RestController
public class MsgController {

    @Resource
    private ReadService readService;

    @PostMapping("readMsg")
    public ResponseBean doReadMsg(@RequestBody MsgDto msgDto) {
        MsgType msgType = getMsgType(msgDto);
        readService.read(msgType, msgDto.getUserId(), msgDto.getMsgId());
        return new ResponseBean();
    }

    @PostMapping("isRead")
    public ResponseBean getIsRead(@RequestBody MsgDto msgDto) {
        MsgType msgType = getMsgType(msgDto);
        return new ResponseBean(readService.isRead(msgType, msgDto.getUserId(), msgDto.getMsgId()));
    }

    private MsgType getMsgType(MsgDto msgDto) {
        MsgType msgType;
        switch (msgDto.getMsgType()) {
            case 1:
                msgType = MsgType.News;
                break;
            case 2:
                msgType = MsgType.Video;
                break;
            case 3:
                msgType = MsgType.Goods;
                break;
            default:
                throw new IllegalArgumentException("msgType is wrong");
        }
        return msgType;
    }
}
